package dao;

import util.Time;
import util.db.MySqlConn;
import factory.AkimimiLineItemFactory;
import factory.defined.ShippingMethod;

public class OrderItem {

	public static void MappingOrderItems(int order_id)
	{
		for(AkimimiLineItemFactory lineItems: AkimimiLineItem.getAkimimiLineItem(order_id))
		{
			String modifyid = lineItems.getUpdate_id();
			String tracking_unm = lineItems.getExpress_info();
			String express_company = lineItems.getExpress_company();
			int quantity = lineItems.getQuantity();
			
			int shipping_method_id  = ShippingMethod.GetShippingIdByName(express_company);
			
			int modify_id = AkimimiOrder.GetIdByName(modifyid);	

			int item_id = 0;
			
			int order_item_id = InsertOrderItems(lineItems, item_id, modify_id);
			
			if(order_item_id > 0){
				int package_id = OrderPackage.InsertOrderPackage(tracking_unm, shipping_method_id, order_id);
				if(package_id > 0){
					int package_item_id = OrderPackageItem.InsertOrderPackageItem(package_id, order_item_id, quantity);
					if(package_item_id > 0){
						System.out.println("[" + Time.nowTime() + "]: " + " ^_^ insert Order_items OK !");
						System.out.println("[" + Time.nowTime() + "]: " + " ^_^ insert Order_packages OK !");
						System.out.println("[" + Time.nowTime() + "]: " + " ^_^ insert Order_package_Items OK !");
					}else{
						System.out.println("[" + Time.nowTime() + "]: " + " @_@ insert Order_package_Items error !");
					}
				}else{
					System.out.println("[" + Time.nowTime() + "]: " + " @_@ insert Order_packages error !");
				}
			}else{
				System.out.println("[" + Time.nowTime() + "]: " + " @_@ insert Order_Items error !");
			}
		}
	}
	
	public static int InsertOrderItems(Object lineItems, int item_id, int modify_id)
	{
		AkimimiLineItemFactory item = (AkimimiLineItemFactory) lineItems;
		String sql = "insert into order_items(id, order_id, item_id, status, " +
				"quantity, sale_price,deal_price, discount, tax," +
				"cheque_rule, cheque_status, ship_profit, order_profit, upc, item_name," +
				"modified, modify_id, created, create_id) values (" +
				item.getId() + "," + item.getOrder_id() + "," + item_id + "," + 1 + "," +
				item.getQuantity() + "," + item.getPrice() + "," + item.getPrice() + "," +  0 + "," + item.getTax() + "," + 
				0 + "," + item.getInvoice() + "," + 0 + "," + 0 + ",'" + item.getProduct_code() + "','" + item.getProduct_title() + "','" +  
				item.getUpdate_time() + "'," + modify_id + ",'" + Time.nowTime() + "'," + 0 + ")";
		System.out.println("[" + Time.nowTime() + "]: " + sql);
		MySqlConn  mysqlConn = new MySqlConn();
		int id = mysqlConn.executeGetid(sql);
		mysqlConn.closeConn();
		return id;
	}
}
